package com.cdu.xinmiao.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.cdu.xinmiao.data.Task;

import java.util.List;

import com.cdu.xinmiao.R;


public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.TaskViewHolder> {
    private List<Task> taskList;

    public TaskAdapter(List<Task> taskList) {
        this.taskList = taskList;
    }

    @NonNull
    @Override
    public TaskViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.task_item, parent, false);
        return new TaskViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull TaskViewHolder holder, int position) {
        Task task = taskList.get(position);
        holder.bind(task);
    }

    @Override
    public int getItemCount() {
        return taskList.size();
    }

    static class TaskViewHolder extends RecyclerView.ViewHolder {
        private TextView textTaskDetail;
        private CheckBox checkboxTaskComplete;

        TaskViewHolder(@NonNull View itemView) {
            super(itemView);
            textTaskDetail = itemView.findViewById(R.id.text_task_detail);
            checkboxTaskComplete = itemView.findViewById(R.id.checkbox_task_complete);
        }

        void bind(Task task) {
            textTaskDetail.setText(task.getDetail());
            checkboxTaskComplete.setChecked(task.isComplete());
        }
    }
}
